// @flow
import React, { Component } from 'react'
import { Button, Modal, Icon, Popconfirm } from 'antd'
import styles from './DeviceActions.css'
import PartAdd from './PartAdd'
import PartEdit from './PartEdit'

type Props = {
  data: Object,
  history: Object,
  deletPart: Function,
  addPart: Function,
  editPart: Function
}
type State = {
  visible: boolean,
  visibleAdd: boolean,
  visibleEdit: boolean
}
class DeviceActions extends Component {
  props: Props
  state: State
  editDevice: Function
  toEdit: Function
  toDeviceAdd: Function
  handleCancel: Function
  handleOk: Function
  constructor (props: Props) {
    super(props)
    this.state = {
      visible: false,
      visibleAdd: false,
      visibleEdit: false
    }
    this.editDevice = this.editDevice.bind(this)
    this.toEdit = this.toEdit.bind(this)
    this.toDeviceAdd = this.toDeviceAdd.bind(this)
    this.handleCancel = this.handleCancel.bind(this)
    this.handleOk = this.handleOk.bind(this)
  }

  handleOk () {
    this.setState({
      visible: true
    })
  }

  handleCancel () {
    this.setState({
      visible: false,
      visibleAdd: false,
      visibleEdit: false
    })
  }

  toDeviceAdd () {
    const { history, data } = this.props
    history.push({
      pathname: '/business/device-point-item',
      state: {
        data: data
      }
    })
  }

  editDevice () {
    const { data, history } = this.props
    history.push({
      pathname: '/business/device-edit',
      state: {
        data
      }
    })
  }

  toEdit () {
    const { history, data } = this.props
    history.push({
      pathname: '/business/device-pointer',
      state: {
        recordId: data.id,
        data: data
      }
    })
  }

  handleDelete = () => {
    const { data } = this.props
    this.props.deletPart(data)
  }
  toPartAdd = () => {
    this.setState({
      visibleAdd: true
    })
  }
  toPartEdit = () => {
    this.setState({
      visibleEdit: true
    })
  }
  render () {
    const { data } = this.props
    // 判断是否有文件
    const fileList = data.fileList
    let showFile = false
    if (fileList.length > 0) {
      showFile = true
    }
    // 判断是设备还是部件
    let isDevice = true
    if (data.parentId) {
      isDevice = false
    }
    return (
      <div className={styles['buttons']}>
        <Button
          type='primary'
          size='small'
          onClick={this.toPartAdd}>
          添加部件
          </Button>
        {
          isDevice
          ? <Button
            type='primary'
            size='small'
            onClick={this.editDevice}>
            编辑
            </Button>
          : <Button
            type='primary'
            size='small'
            onClick={this.toPartEdit}>
            编辑
            </Button>
        }
        {
          isDevice
          ? <Button
            type='primary'
            size='small'
            onClick={this.toDeviceAdd}>
            测点
            </Button>
          : <Button
            type='primary'
            size='small'
            onClick={this.toDeviceAdd}>
            测点
            </Button>
        }
        {
          isDevice
          ? false
          : <Popconfirm title='确定要删除此部件吗'
            onConfirm={this.handleDelete}
            >
            <Button size='small' type='danger'
              >删除</Button>
          </Popconfirm>
        }
        {
          // <Button
          //   size='small'
          //   onClick={this.toEdit}>
          //   测点
          //   </Button>
        }
        {
          showFile
            ? <a title='下载附件' onClick={this.handleOk} >
              <Icon type='paper-clip' style={{ fontSize: 16 }} />
            </a>
            : ''
        }
        <Modal
          title='下载附件'
          visible={this.state.visible}
          onCancel={this.handleCancel}
          footer={null}
        >
          {
            fileList.map((d, i) => {
              return <a
                style={{ display: 'block', padding: 3 }}
                key={i}
                href={d.fileUrl}
                target='_blank' rel='noopener noreferrer'
                title={d.fileName}>
                {d.fileName}
              </a>
            })
          }
        </Modal>
        <Modal
          title='添加部件'
          visible={this.state.visibleAdd}
          onCancel={this.handleCancel}
          footer={null}
        >
          <PartAdd data={data} handleCancel={this.handleCancel} addPart={this.props.addPart} />
        </Modal>
        <Modal
          title='编辑部件'
          visible={this.state.visibleEdit}
          onCancel={this.handleCancel}
          footer={null}
        >
          <PartEdit data={data} handleCancel={this.handleCancel} editPart={this.props.editPart} />
        </Modal>
      </div>
    )
  }
}

export default DeviceActions
